package com.sduept.nwld.dataserver.manager.protection;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sduept.bigdata.fault.protection.model.ProtectChannelAlarm;
import com.sduept.bigdata.fault.protection.model.ProtectDeviceAlarm;
import com.sduept.bigdata.fault.protection.service.ProtectChannelAlarmQueryManager;
import com.sduept.bigdata.fault.protection.service.ProtectDeviceAlarmQueryManager;
import com.sduept.cim.model.protection.ProtectionEquipment;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.manager.cim.CimManager;

@Service
public class ProtectStatusManager {
	@Autowired
	private ProtectChannelAlarmQueryManager pcaqm;
	@Autowired
	private ProtectDeviceAlarmQueryManager pdaqm;
	@Autowired
	private CimManager cm;
	public List<ProtectionEquipment> getProtectStatusByLineId(String...line_id){
		Map<String,ProtectionEquipment> map = new HashMap<String, ProtectionEquipment>();
		List<ProtectionEquipment> protectionEquipmentList= cm.getProtectionEquipmentsByAclineId(line_id);
		for(ProtectionEquipment pe:protectionEquipmentList){
			ProtectionEquipment newpe = new ProtectionEquipment();
			try {
				BeanUtils.copyProperties(newpe, pe);
				map.put(pe.getPtId(), newpe);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		List<ProtectChannelAlarm> protectChannelAlarmList = pcaqm.getUnRecoveryProtectChannelAlarmByLineId(line_id, null, null);
		for(ProtectChannelAlarm pa:protectChannelAlarmList){
			ProtectionEquipment pe = map.get(pa);
			if(pe!=null)
				pe.setmCommuStatus(-1);
		}
		List<ProtectDeviceAlarm> protectDeviceAlarmList  =  pdaqm.getProtectDeviceAlarmByLineId(line_id, null, null);
		for(ProtectDeviceAlarm pa:protectDeviceAlarmList){
			ProtectionEquipment pe = map.get(pa);
			if(pe!=null)
				pe.setmRunStatus(-1);
		}
		return filter(map.values());
	}
	public List<ProtectionEquipment> getProtectStatusByStationId(String...station_id){
		Map<String,ProtectionEquipment> map = new HashMap<String, ProtectionEquipment>();
		List<ProtectionEquipment> protectionEquipmentList= cm.getMainProtectionEquipmentsByStationId(station_id);
		for(ProtectionEquipment pe:protectionEquipmentList){
			ProtectionEquipment newpe = new ProtectionEquipment();
			try {
				BeanUtils.copyProperties(newpe, pe);
				map.put(pe.getPtId(), newpe);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		List<ProtectChannelAlarm> protectChannelAlarmList = pcaqm.getUnRecoveryAndHadBhIdProtectChannelAlarmByStationId(station_id, null, null);
		for(ProtectChannelAlarm pa:protectChannelAlarmList){
			ProtectionEquipment pe = map.get(pa.getBhId());
			if(pe!=null)
				pe.setmCommuStatus(-1);
		}
		List<ProtectDeviceAlarm> protectDeviceAlarmList  =  pdaqm.getProtectDeviceAlarmByStationId(station_id, null, null);
		for(ProtectDeviceAlarm pa:protectDeviceAlarmList){
			ProtectionEquipment pe = map.get(pa.getProtectId());
			if(pe!=null)
				pe.setmRunStatus(-1);
		}
		return filter(map.values());
	}
	private List<ProtectionEquipment> filter(Collection<ProtectionEquipment> col){
		List<ProtectionEquipment> result = new ArrayList<ProtectionEquipment>();
		for(ProtectionEquipment pe:col){
			if((pe.getmRunStatus() != null && pe.getmRunStatus()==-1 ) || (pe.getmCommuStatus() != null && pe.getmCommuStatus()==-1)){
				result.add(pe);
			}
		}
		return result;
	}
}

